<template>
  <div class="channel-edit">
    <!-- 当前登陆用户已经订阅的频道 -->
    <div class="channel">
      <van-cell title="我的频道" :border="false">
        <van-button
          size="mini"
          type="info"
          class="editBtn"
          @click="isEditing = !isEditing"
          >{{ isEditing ? "取消" : "编辑" }}</van-button
        >
      </van-cell>
      <van-grid>
        <van-grid-item
          v-for="(channel, idx) in channels"
          :key="idx"
          @click="hClickMyChannel(idx)"
          :class="{ red: idx === curIndex }"
        >
          <span>{{ channel.name }}</span>
          <van-icon
            name="cross"
            v-show="isEditing && idx !== 0"
            class="btn"
          ></van-icon>
        </van-grid-item>
      </van-grid>
    </div>
    <!-- 当前登陆用户没有订阅的频道 -->
    <div class="channel">
      <van-cell title="可选频道" :border="false"></van-cell>
      <van-grid>
        <van-grid-item
          v-for="channel in recommendChannels"
          :key="channel.id"
          @click="hAddChannel(channel)"
        >
          <span>{{ channel.name }}</span>
        </van-grid-item>
      </van-grid>
    </div>
  </div>
</template>

<script>
import { delChannel } from '../../api/channel'
import { addChannels, getAllChannels } from '../../api/article'
export default {
  name: 'ChannelEdit',
  data () {
    return {
      isEditing: false, // 是否处于编辑状态
      allChannels: []
    }
  },
  // 接受列表，和索引
  props: ['channels', 'curIndex'],
  created () {
    this.loadAllChannels()
  },
  methods: {
    async loadAllChannels () {
      const res = await getAllChannels()
      this.allChannels = res.data.data.channels
    },
    // 用户点击了我的频道
    hClickMyChannel (idx) {
      if (this.isEditing) {
        // 做删除
        // console.log(123)
        this.doDeleteChannel(idx)
      } else {
        // 当前频道的下标
        this.$emit('change-channel', idx)
      }
    },
    async doDeleteChannel (idx) {
      // console.log(idx)
      // 根据下标找到id
      const channelId = this.channels[idx].id
      console.log(channelId)
      // 做删除
      try {
        const res = await delChannel(channelId)
        console.log(res)
        // 我的频道少一项，删除当前项
        this.channels.splice(idx, 1)
        this.$toast.success('删除成功')
        if (idx < this.curIndex) {
          this.$emit('fixed-active-index')
        }
      } catch (err) {
        console.log(err)
      }
    },
    async hAddChannel (channel) {
      console.log('添加', channel)
      try {
        const res = await addChannels([channel])
        this.channels.push(channel)
        // this.$toast.success('添加频道成功')
        console.log(res)
      } catch (err) {
        console.log(err)
        // this.$toast.fail('添加频道失败')
      }
    }
  },
  computed: {
    recommendChannels () {
      const arr = this.allChannels.filter(channel => {
        // eslint-disable-next-line eqeqeq
        const idx = this.channels.findIndex(item => item.id == channel.id)

        if (idx === -1) {
          return true
        }
      })
      return arr
    }
  }
}
</script>

<style lang="less" scoped>
.channel {
  padding: 15px;
  font-size: 14px;
}
.btn {
  position: absolute;
  top: 0;
  right: 0;
  font-size: 24px;
}
// 高亮显示
.red {
  color: red;
  font-weight: bold;
}
.editBtn {
  color: #fff !important;
  margin-right: 10px;
  padding: 0 10px;
  border-color: green;
}
</style>
